<template>
  <div class="dashboard">
    <h1>智能家居控制中心</h1>
    
    <div class="alert" v-if="error">
      {{ error }}
    </div>
    
    <div class="grid-container">
      <SensorCard
        title="温度"
        :value="temperature"
        unit="°C"
        icon="mdi:thermometer"
        :comfort-level="comfortLevel"
        :timestamp="lastUpdate"
      />
      
      <SensorCard
        title="湿度"
        :value="humidity"
        unit="%"
        icon="mdi:water-percent"
        :comfort-level="comfortLevel"
        :timestamp="lastUpdate"
      />
      
      <DeviceControl />
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useSmartHomeStore } from '../stores/smartHome'
import SensorCard from './SensorCard.vue'
import DeviceControl from './DeviceControl.vue'

const store = useSmartHomeStore()
const lastUpdate = ref(new Date());

const temperature = computed(() => store.temperature)
const humidity = computed(() => store.humidity)
const comfortLevel = computed(() => store.comfortLevel)
const error = computed(() => store.error)

// 更新时间
setInterval(() => {
  lastUpdate.value = new Date()
}, 1000)
</script>

<style scoped>
.dashboard {
  max-width: 1500px;
  margin: 0 auto;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
  border-radius: 16px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.dashboard h1 {
  color: #333;
  text-align: center;
  margin-bottom: 30px;
  font-size: 2.5rem;
  font-weight: 600;
}

.alert {
  padding: 12px;
  background: #F44336;
  color: white;
  border-radius: 8px;
  margin-bottom: 20px;
  text-align: center;
  font-weight: 500;
}

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 350px)); /* 限制最大宽度 */
  gap: 20px;
  justify-content: center; /* 让卡片居中，避免拉伸 */
}

@media (min-width: 1200px) {
  .grid-container {
    grid-template-columns: repeat(3, 300px); /* 固定三列 */
  }
}

/* @media (min-width: 1024px) {
  .grid-container {
    grid-template-columns: repeat(3, 1fr);
  }
} */
</style>